﻿using Infrastructure.Attributes.Ioc;
using Infrastructure.Model.User;
using Microsoft.AspNetCore.Http;
using ShareCode.Dto;
using ShareCode.Entity;
using ShareCode.Util.Extension;
using ShareCode.Util.Helper;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dao
{
    public class BaseContainDateDao<T> : BaseBaseDao<T> where T : BaseContainDateEntity, new()
    {
        [Autowired]
        public IHttpContextAccessor? HttpContext { set; get; }

        public override void DataExecuting(object oldValue, DataFilterModel entityInfo)
        {
            base.DataExecuting(oldValue, entityInfo);

            if (HttpContext == null) return;

            var UserInfo = UserSessionInfoModel.GetSession(HttpContext);

            //inset生效
            if (entityInfo.OperationType == DataFilterType.InsertByObject)
            {
                if (entityInfo.PropertyName == "CreateTime")
                {
                    var model = (BaseContainDateEntity)entityInfo.EntityValue;

                    model.CreateTime = DateTimeHelper.GetTimeStamp();
                    model.UpdateTime = model.CreateTime;
                    model.Creator = UserInfo == default ? 0 : UserInfo.EntryInformationId;
                    model.Updater = model.Creator;
                }
            }

            //update生效        
            if (entityInfo.OperationType == DataFilterType.UpdateByObject)
            {
                if (entityInfo.PropertyName == "UpdateTime")
                {
                    var model = (BaseContainDateEntity)entityInfo.EntityValue;

                    model.UpdateTime = DateTimeHelper.GetTimeStamp();
                    model.Updater = UserInfo == default ? 0 : UserInfo.EntryInformationId;
                }

            }
        }

        public override Expressionable<T> GetSearchExpression(PagingDto pars)
        {
            if (pars.SearchField.HasLong("CreateTimeStart") && !pars.SearchField.HasLong("CreateTimeEnd"))
            {
                pars.SearchField["CreateTimeEnd"] = pars.SearchField["CreateTimeStart"];
            }

            if (pars.SearchField.HasLong("UpdateTimeStart") && !pars.SearchField.HasLong("UpdateTimeEnd"))
            {
                pars.SearchField["UpdateTimeEnd"] = pars.SearchField["UpdateTimeStart"];
            }

            var whereExpression = base.GetSearchExpression(pars)
          .AndIF(pars.SearchField.HasLong("Creator"), t => t.Creator == pars.SearchField.GetLong("Creator"))
          .AndIF(pars.SearchField.HasLong("Updater"), t => t.Updater == pars.SearchField.GetLong("Updater"))
          .AndIF(pars.SearchField.HasInt("CreateTimeStart"), t => t.CreateTime >= pars.SearchField.GetLong("CreateTimeStart") && t.CreateTime <= pars.SearchField.GetLong("CreateTimeEnd"))
          .AndIF(pars.SearchField.HasInt("UpdateTimeStart"), t => t.CreateTime >= pars.SearchField.GetLong("UpdateTimeStart") && t.CreateTime <= pars.SearchField.GetLong("UpdateTimeEnd"));

            return whereExpression;
        }

    }
}
